package operation;

import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration {
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner in = new Scanner(System.in);
        System.out.println("请输入删除图书的名字");
        String name = in.nextLine();
        for (int i = 0; i < bookList.getUseSize(); i++) {
            if(bookList.getBook(i).getName().equals(name)) {
                for (int j = i; j < bookList.getUseSize() - 1; j++) {
                    bookList.setBook(j, bookList.getBook(j + 1));
                }
                System.out.println("删除成功");
                //这个要写，不然会保留最后一个的下标，最后一个的内存就不会自动清除
                bookList.setBook(bookList.getUseSize() - 1, null);
                bookList.setUseSize(bookList.getUseSize() - 1);
                return;
            }
        }
        System.out.println("未找到这本书，请重新核对一下");
    }
}